package com.guangaiboen.attendance.wx.web;

import com.guangaiboen.attendance.db.dao.AttendanceUserMapper;
import com.guangaiboen.attendance.db.domain.AttendanceUser;
import com.guangaiboen.attendance.db.domain.resp.AttendanceUserResp;
import com.guangaiboen.attendance.db.service.AttendanceUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.guangaiboen.attendance.core.util.ResponseUtil;
import com.guangaiboen.attendance.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/wx/user")
@Validated
public class WxUserController {
    private final Log logger = LogFactory.getLog(WxUserController.class);

    @Autowired
    private AttendanceUserService userService;



    /**
     * 用户个人页面数据
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("index")
    public Object list(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        Map<Object, Object> data = new HashMap<Object, Object>();
        return ResponseUtil.ok(data);
    }




    /**
     * 用户个人页面数据
     * <p>
     * 目前是用户订单统计信息
     *
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("getUserInfo")
    public Object getUserInfo(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        AttendanceUserResp userInfo = userService.getUserInfo(userId);
        if(userInfo!=null){
            Byte gender = userInfo.getGender();
            if(gender==1){
                userInfo.setRealGender("男");
            }else if(gender==2){
                userInfo.setRealGender("女");
            }
        }
        return ResponseUtil.ok(userInfo);
    }

    /**
     * 修改个人信息
     * @param userId
     * @return
     */
    @PostMapping("updateUser")
    public Object updateUser(@RequestBody AttendanceUser attendanceUser) {
        if (attendanceUser.getId() == null) {
            return ResponseUtil.unlogin();
        }
        userService.updateUser(attendanceUser);
        return ResponseUtil.ok();
    }


}
